﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bluyel.Core;

namespace Bluyel_Samples_VS10
{
    /// <summary>
    /// Code samples to see how to create:
    /// Var/Const Data Structure with members basic types.
    /// </summary>
    public class Sample_VarConst_Struct_M_Basic
    {
        //---------------------------------------------------------------------
        /// <summary>
        /// Create a struct with an int as member.
        /// </summary>
        public void CreateStruct_M_Int_String_Bool()
        {
            Console.WriteLine("\n----Create a data struct with 3 members:\n");

            // create the core and define all built-in types
            BluyelCore core = new BluyelCore();
            core.TypeBuilder.DefineAllTypes();

            // Create a var data structure, with some data basic type  
            // struct st
            //   int a
            //   string s= 'hello'
            //   bool b
            // end
            Console.WriteLine("Create a data structure:");
            Console.WriteLine("struct st\n  int a\n  string s ='hello'\n  bool b\nend");
            IDStruct dataSt = core.DataBuilder.CreateVarStruct("st");

            // add members
            DInt varSt_a = core.DataBuilder.AddVarStructMemberInt(dataSt, "a");
            DString varSt_s = core.DataBuilder.AddVarStructMemberString(dataSt, "s","hello");
            DBool varSt_b = core.DataBuilder.AddVarStructMemberBool(dataSt, "b");

            Console.WriteLine("Member count=" + dataSt.ListData.Count());

            // display members
            Console.WriteLine("\nDisplay Members:");
            foreach (IDataNamed data in dataSt.ListData)
                Console.WriteLine("Name= " + data.Name + ", Type= "+ data.Type);

            // remove a member
            bool res = core.DataBuilder.RemoveVarStructMember(dataSt, "b");
            Console.WriteLine("\nRemove the member b, result=" + res);
            Console.WriteLine("Member count=" + dataSt.ListData.Count());
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Create a struct with an int as member.
        /// </summary>
        public void CreateStruct_M_FindMemberInt()
        {
            Console.WriteLine("\n----Create a data struct, find a member:\n");

            // create the core and define all built-in types
            BluyelCore core = new BluyelCore();
            core.TypeBuilder.DefineAllTypes();

            // Create a var data structure, with some data basic type  
            // struct st
            //   int a
            //   string s= 'hello'
            //   bool b
            //   const int c=34
            // end
            Console.WriteLine("Create a data structure:");
            Console.WriteLine("struct st\n  int a\n  string s ='hello'\n  bool b\n  const int c=34\nend");
            IDStruct dataSt = core.DataBuilder.CreateVarStruct("st");

            // add members
            DInt varSt_a = core.DataBuilder.AddVarStructMemberInt(dataSt, "a");
            DString varSt_s = core.DataBuilder.AddVarStructMemberString(dataSt, "s", "hello");
            DBool varSt_b = core.DataBuilder.AddVarStructMemberBool(dataSt, "b");
            DInt varSt_c = core.DataBuilder.AddConstStructMemberInt(dataSt, "c", 34);

            // find a member, by the name
            Console.WriteLine("Find member by the name: a");
            IDataNamed aFind = dataSt.FindMember("a");
            Console.WriteLine("Type of a: " + aFind.Type);
            DInt aInt = aFind as DInt;
            Console.WriteLine("Value of a: " + aInt.Val);

            // change the value
            Console.WriteLine("\nChange the value of: st.a <- 12");
            core.DataBuilder.SetVal(aInt, 12);


            // find a member, by the name
            Console.WriteLine("\nAgain, Find member by the name: a");
            IDataNamed aFind2 = dataSt.FindMember("a");
            Console.WriteLine("Type of a: " + aFind2.Type);
            DInt aInt2 = aFind2 as DInt;
            Console.WriteLine("Value of a: " + aInt2.Val);

            // try modify the const int c=34
            bool res = core.DataBuilder.SetVal(varSt_c, 67);
            Console.WriteLine("Try modify the member const Int c, result=" + res);
            Console.WriteLine("Value of Const int c= " + varSt_c.Val);                       
        }

    }
}
